package com.deltabravomedia.theoldtoad;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends ListActivity {
	private static final int ACTIVITY_FOOD=0;
	private static final int ACTIVITY_DRINKS=1;
	private static final int ACTIVITY_CREW=2;
	private static final int ACTIVITY_EVENTS=3;
	private static final int ACTIVITY_INFO=4;
	private static final int ACTIVITY_MAP=5;
	private static final int ACTIVITY_CONTACT=6;
	private static final int ACTIVITY_PREFERENCES=7;

	private static final int SETTINGS_ID=Menu.FIRST;
	
	private static final String TAG = "Main";

	private ProgressDialog m_ProgressDialog = null; 
    private ArrayList<Item> m_items = null;
    private ItemAdapter m_adapter;
    private Runnable viewItems;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.listviewwithbackgroundimage);
        this.setTitle("The Old Toad");
        m_items = new ArrayList<Item>();
        this.m_adapter = new ItemAdapter(this, R.layout.row, m_items);
                setListAdapter(this.m_adapter);
   
        viewItems = new Runnable(){
            @Override
            public void run() {
                getItems();
            }
        };
        
        Thread thread =  new Thread(null, viewItems, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(Main.this,    
              "Please wait...", "Retrieving data ...", true);
    }
        
    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem settingsItem = menu.add(0, SETTINGS_ID, 0, R.string.menu_settings);
        settingsItem.setIcon(R.drawable.options);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected (int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case SETTINGS_ID:
            Intent i = new Intent(this, Preferences.class);
            startActivityForResult(i, ACTIVITY_PREFERENCES);
            return true;        
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
    
	@Override
    protected void onListItemClick (ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        Log.i(TAG, m_items.get(position).getItemName());
        
        String item = m_items.get(position).getClassName();
        
        if (item.equals("Food")) {
            Intent i = new Intent(this, Food.class);
            startActivityForResult(i, ACTIVITY_FOOD);
        } else if (item.equals("Drinks")) {
            Intent i = new Intent(this, Drinks.class);
            startActivityForResult(i, ACTIVITY_DRINKS);
        } else if (item.equals("Crew")) {
            Intent i = new Intent(this, Crew.class);
            startActivityForResult(i, ACTIVITY_CREW);
        } else if (item.equals("Events")) {
            Intent i = new Intent(this, Events.class);
            startActivityForResult(i, ACTIVITY_EVENTS);
        } else if (item.equals("Info")) {
            Intent i = new Intent(this, Info.class);
            startActivityForResult(i, ACTIVITY_INFO);
        } else if (item.equals("Map")) {
            Intent i = new Intent(this, Map.class);
            startActivityForResult(i, ACTIVITY_MAP);
        } else if (item.equals("Contact")) {
            Intent i = new Intent(this, Contact.class);
            startActivityForResult(i, ACTIVITY_CONTACT);
        }
	}
	
    private Runnable returnRes = new Runnable() {
        @Override
        public void run() {
            if(m_items != null && m_items.size() > 0){
                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_items.size();i++)
                m_adapter.add(m_items.get(i));
            }
            m_ProgressDialog.dismiss();
            m_adapter.notifyDataSetChanged();
        }
    };
    
    private class ItemAdapter extends ArrayAdapter<Item> {

        private ArrayList<Item> items;

        public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> items) {
	        super(context, textViewResourceId, items);
	        this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
			    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			    view = vi.inflate(R.layout.row, null);
			}
			Item item = items.get(position);
			if (item != null) {
			        TextView topText = (TextView) view.findViewById(R.id.toptext);
			        if (topText != null) {
			              topText.setText( item.getItemName());                            
			        }

			        ImageView iconView = (ImageView) view.findViewById(R.id.icon);
			        if (iconView != null) {
			        	int resID = getResources().getIdentifier(item.getIconName(), "drawable", "com.deltabravomedia.theoldtoad");
			            iconView.setImageResource(resID);                
			        }
			}
			return view;
        }
    }
    
    private void getItems(){
		m_items = new ArrayList<Item>();

		Item item0 = new Item();
		item0.setItemName("Food");
		item0.setIconName("food");
		item0.setClassName("Food");

		Item item1 = new Item();
		item1.setItemName("Drinks");
		item1.setIconName("drinks");
		item1.setClassName("Drinks");

		Item item3 = new Item();
		item3.setItemName("Crew");
		item3.setIconName("crew");
		item3.setClassName("Crew");
		
		Item item4 = new Item();
		item4.setItemName("Events");
		item4.setIconName("events");
		item4.setClassName("Events");
		
		Item item5 = new Item();
		item5.setItemName("Info");
		item5.setIconName("info");
		item5.setClassName("Info");
		
		Item item6 = new Item();
		item6.setItemName("Map");
		item6.setIconName("map");
		item6.setClassName("Map");

		Item item7= new Item();
		item7.setItemName("Contact");
		item7.setIconName("contact");
		item7.setClassName("Contact");
		
        m_items.add(item0);
        m_items.add(item1);
        m_items.add(item3);
        m_items.add(item4);
        m_items.add(item5);
        m_items.add(item6);
        m_items.add(item7);

        Log.i("ARRAY", ""+ m_items.size());
          
    	runOnUiThread(returnRes);

    }
}